<?php

namespace App\Console\Commands;

use App\Customer;
use App\Lead;
use App\Record;
use App\User;
use App\UserFenshu;
use App\UserFenshuSetting;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;

class UserFenshuCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'user:fenshu';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        User::where('status',1)->chunkById(100,function ($users){
            foreach ($users as $item){
                $user=User::find($item->id);
                $userFenshu=UserFenshu::where('time','=',Carbon::now()->format('Y-m'))
                ->where('user_id',$user->id)
                ->first();
                if (!$userFenshu){
                    $userFenshu=new UserFenshu();
                    $userFenshu->time=Carbon::now()->format('Y-m');
                    $userFenshu->user_id=$user->id;
                }
                $userFenshu->daofang=$this->getDaofang($user);
                $userFenshu->xinren=$user->is_new;
                $userFenshu->chengjiao=$this->getChengjiao($user);
                $userFenshu->genjin=$this->getGenjin($user);
                $userFenshu->fenshu=$this->getFenshu($userFenshu);
                $userFenshu->lead=$this->lead($user);
                $userFenshu->save();
                $this->info($userFenshu->toJson());
            }
        });
    }
    protected function  getFenshu(UserFenshu $userFenshu){
        $userFenshuSetting=UserFenshuSetting::find(1);
        return $userFenshu->daofang*$userFenshuSetting->daofang_fenshu
            +$userFenshu->xinren*$userFenshuSetting->new_fenshu
            +$userFenshuSetting->base
            +$userFenshu->genjin*$userFenshuSetting->genjin_fenshu
            +$userFenshu->chengjiao*$userFenshuSetting->chengjiao_fenshu;
    }
    protected  function getDaofang(User $user){
       return  $customer=Customer::where('daofang_time','>',Carbon::now()->firstOfMonth()->timestamp)

                ->where('owner_user_id','=',$user->id)
                ->count();
    }
    protected  function getGenjin(User $user){
     return   $record=Record::where('types','crm_customer')
         ->where('create_time','>',Carbon::now()->firstOfMonth()->timestamp)
         ->where('create_user_id',$user->id)->count();
    }
    protected function getChengjiao(User $user){
         return    $customer=Customer::where('deal_status','=','已成交')
             ->where('deal_time','>',Carbon::now()->firstOfMonth()->timestamp)
            ->where('owner_user_id','=',$user->id)
            ->count();
    }
    public function lead(User $user){
        return    $customer=Lead::
            where('create_time','>',Carbon::now()->firstOfMonth()->timestamp)
            ->where('owner_user_id','=',$user->id)
            ->count();
    }
}
